import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login.vue"
import Home from "../components/Home.vue"
import Welcome from "../components/Welcome.vue"
import User from "../components/users/User.vue"
import Rights from "../components/power/Rights.vue"
import Roles from "../components/power/Roles.vue"
import Cata from "../components/goods/Cata.vue"
import Params from "../components/goods/Params.vue"
import List from "../components/goods/List.vue"
import Add from "../components/goods/Add.vue"
import Order from "../components/order/Order.vue"
import Report from "../components/report/Report.vue"

Vue.use(VueRouter)

const routes = [
  // 对路径实现重定向
  // {path:"/",redirect:"/login"},

  {path:"/login",name:"login",component:Login},
  {path:"/home",name:"home",component:Home,
  redirect:"/welcome",
  children:[{path:"/welcome",name:"welcome",component:Welcome},
  {path:"/users",name:"users",component:User},
  {path:"/users",name:"users",component:User},
  {path:"/rights",name:"rights",component:Rights},
  {path:"/roles",name:"roles",component:Roles},
  {path:"/categories",name:"categories",component:Cata},
  {path:"/params",name:"params",component:Params},
  {path:"/goods",name:"list",component:List},
  {path:"/goods/add",name:"add",component:Add},
  {path:"/orders",name:"orders",component:Order},
  {path:"/reports",name:"reports",component:Report},

]},

  
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  if(to.name=="login") return next();
  // 另一种情况访问的就是home页，需要拿到token去做验证
  const tokenStr=window.sessionStorage.getItem("token")
  // 若为空，强制跳转到登录页面(语法不够严谨，未与服务端拿到的token做验证)
  if(!tokenStr) return next("/login")
  next()
})

export default router
